package org.rcgwt.client.ui.grid.internal;

import java.util.Iterator;

import org.rcgwt.client.ui.grid.model.AbstractGroupDataModel;
import org.rcgwt.client.ui.grid.model.IGroup;
import org.rcgwt.client.ui.grid.model.IGroupDataModel;

/**
 * Default GroupModel when the user doesn't define a {@link IGroupDataModel}
 * Just one group
 * 
 * @author Matthieu ROBERT & Olivier Oeuillot
 * 
 */
public class SingleGroupModel extends AbstractGroupDataModel implements
        ISingleGroupDataModel {

    private static final String DEFAULT_GROUP_NAME = "default";

    private final IGroup singleGroup;

    public SingleGroupModel() {
        singleGroup = new BasicGroup();
    }

    /**
     * Always the same Group, because just one by default
     * 
     * @throws IllegalStateException
     *             No more one group
     */
    @Override
    public IGroup getGroupByRowModelIndex(int groupIndex) {
        if (groupIndex == 0) {
            return singleGroup;
        }

        throw new IllegalStateException("No more one group");
    }

    @Override
    public int getGroupRowCount(IGroup group) {
        return dataModel.getRowCount();
    }

    /**
     * Just one group by default
     * 
     * @see org.rcgwt.client.ui.grid.model.IGroupDataModel#getGroupCount()
     * 
     @Override public int getGroupCount(int start, int end) { return 1; }
     */

    @Override
    public Iterator<Integer> getGroupModelIndexes(int startRowModelIndex,
            int rowCount) {
        return new Iterator<Integer>() {
            private int count = 0;

            @Override
            public boolean hasNext() {
                return count < 1;
            }

            @Override
            public Integer next() {
                count = 1;

                return new Integer(0);
            }

            @Override
            public void remove() {

            }

        };
    }

    @Override
    public Iterator<Integer> getRowModelIndexes(IGroup group,
            final int startRowModelIndex, final int rowCount) {
        return new Iterator<Integer>() {
            private int count = 0;

            @Override
            public boolean hasNext() {

                if (rowCount < 0) {
                    return dataModel.isRowAvailable(startRowModelIndex + count);
                }

                return count < rowCount;
            }

            @SuppressWarnings("boxing")
            @Override
            public Integer next() {
                int index = startRowModelIndex + count;
                count++;

                return index;
            }

            @Override
            public void remove() {

            }

        };
    }

    @Override
    public IGroup getGroupByIndex(int groupModelIndex) {
        assert groupModelIndex == 0 : "Invalid group index";

        return singleGroup;
    }

    @Override
    public int getGroupCount() {
        return 1;
    }

    @Override
    public IGroup getGroup() {
        return singleGroup;
    }

    @Override
    public String getGroupName(IGroup group) {
        return "Main group";
    }
}
